//
//  adViewController.m
//  cooba-iOS
//
//  Created by 郭四虎 on 16/2/22.
//  Copyright © 2016年 YouKu. All rights reserved.
//

#import "adViewController.h"

@interface AdViewController ()<UIWebViewDelegate,WKNavigationDelegate>
{
    WKWebView *web;
    UIProgressView *progressView;
}
@property (weak, nonatomic) IBOutlet UIWebView *myWeb;
@end

@implementation AdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    //_myWeb.backgroundColor = [UIColor clearColor];
    NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
    
    WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
    WKUserContentController *wkUController = [[WKUserContentController alloc] init];
    [wkUController addUserScript:wkUScript];
    
    WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
    wkWebConfig.userContentController = wkUController;

    
//    WKWebViewConfiguration *con = [[WKWebViewConfiguration alloc]init];
//    con.preferences.minimumFontSize = 20;
    web = [[WKWebView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT-64)configuration:wkWebConfig];
    web.navigationDelegate = self;
    
    [web addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
    [self.view addSubview:web];
    
    [web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlstr]]];
    NSLog(@"%@",self.urlstr);
//    [_myWeb setOpaque:NO];
//    [_myWeb loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlstr]]];
    progressView = [[UIProgressView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 10)];
    [web addSubview:progressView];
    
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    if ([keyPath  isEqual: @"estimatedProgress"]) {
        
        [progressView setProgress:web.estimatedProgress animated:YES];
        
        if (web.estimatedProgress == 1) {
            [progressView setProgress:0 animated:NO];
            progressView.hidden = YES;
            
        }else{
            progressView.hidden = NO;
        }
    
        NSLog(@"%F",web.estimatedProgress);
       
        //progressView.setProgress(Float(webView.estimatedProgress), animated: true)
    }
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [_myWeb Loading_0000];
    [_myWeb performSelector:@selector(Hidden) withObject:nil afterDelay:5];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [_myWeb Hidden];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [_myWeb Message:@"请检查网络" HiddenAfterDelay:1];
}


- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation
{
    NSLog(@"start");
    [web Loading_0314];
    [web performSelector:@selector(Hidden) withObject:nil afterDelay:5];
}
- (void)webView:(WKWebView *)webView didCommitNavigation:(null_unspecified WKNavigation *)navigation
{
    NSLog(@"commit");
}
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error
{
    NSLog(@"fail");
    [web Message:@"请检查网络" HiddenAfterDelay:1];
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation
{
    NSLog(@"finish");
    [web Hidden];
}


- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [web removeObserver:self forKeyPath:@"estimatedProgress"];
    if (web.loading) {
        [web stopLoading];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
